package com.apes.scm.pur.priceChange;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.account.account.model.InventoryBatch;
import com.apes.scm.account.tax.model.Tax;
import com.apes.scm.masterdata.party.model.PartyRole;
import com.apes.scm.masterdata.product.model.Product;
import com.apes.scm.masterdata.product.model.ProductUom;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Map;

import static com.apes.framework.util.Arith.round;


/**
 * 功能：采购变价单行数据
 *
 * @author HuJianLin
 * @create 2018-01-25 19:06
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = PriceChangePlatformItem.CONST_TABLE_NAME, indexes =
        {@Index(name = "u_price_change_platForm_i", columnList = "id, old_batch_id", unique = true)})
public class PriceChangePlatformItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "pur_price_change_platform_item";

    @Id
    @Column(name = "id")
    @ApesGenerator(code = CONST_TABLE_NAME, name = "采购变价单行")
    private long id;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    private PartyRole company; //公司

    @NotNull
    @ManyToOne(optional = false)
    @JoinColumn(name = "price_Change_id", referencedColumnName = "id")
    private PriceChangePlatform priceChangePlatform;

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_id", referencedColumnName = "id")
    private Product product; //商品

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "old_batch_id", referencedColumnName = "id")
    private InventoryBatch oldBatch; //原批次

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "new_batch_id", referencedColumnName = "id")
    private InventoryBatch newBatch; //新批次

    @Column(name = "product_uom_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double productUomQty = 0; //数量

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "product_uom_id", referencedColumnName = "id")
    private ProductUom uom; //计量单位

    @Column(name = "old_price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double oldPrice = 0; //原单价

    @Column(name = "new_price_unit", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double newPrice = 0; //新单价

    @Column(name = "amt_without_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double amtWithoutTax = 0; //不含税金额

    @Column(name = "tax_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double taxAmt = 0; //税金

    @Column(name = "amt_with_tax", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double amtWithTax = 0; //含税金额

    @Column(name = "change_balance_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double changeBalanceQty = 0; //变库存数量

    @Column(name = "change_sales_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double changeSalesQty = 0; //变销售数量

    @Column(name = "change_Profit_Loss_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double changeProfitLossQty = 0; //变损益数量

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tax_id", referencedColumnName = "id")
    private Tax tax; //税率

    @Column(name = "invoiced_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    private double invoicedQty = 0; //开票数量

    @Column(name = "balance_qty", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("结存数量")
    private double balanceQty = 0.0;

    public void computeAmt() {
        Map result = this.getTax().computeAll(this.getCompany(), newPrice-oldPrice, productUomQty);
        taxAmt = ((Double) result.get("taxAmt")).doubleValue();
        amtWithoutTax = ((Double) result.get("totalExcluded")).doubleValue();
        amtWithTax = ((Double) result.get("totalIncluded")).doubleValue();
    }


    @Transient  //变价差额含税单价
    private double changeDifference = 0;

    public double getChangeDifference() {
       return newPrice - oldPrice;
   }

    @Transient  //变价差额无税单价
    private double changeDifferenceNoTax = 0;

    public double getChangeDifferenceNoTax() {
        return round((newPrice - oldPrice)/(1+ tax.obtainPercentTypeTax()),3);
    }

}
